<template>
  <div class="container">
    <el-form ref="form" :model="form" :rules="rules" label-width="210px">
      <el-form-item label="名称" prop="name">
        <el-input
            v-model="form.name"
            placeholder="请填写名称"
            clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="发件人昵称" prop="nickName">
        <el-input
            v-model="form.nickName"
            placeholder="请填写发件人昵称"
            clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="发件服务器主机" prop="host">
        <el-input
            v-model="form.host"
            placeholder="请填写发件服务器主机"
            clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="发件服务器账户" prop="username">
        <el-input
            v-model="form.username"
            placeholder="请填写发件服务器账户"
            clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="发件服务器密码" prop="password">
        <el-input
            v-model="form.password"
            placeholder="请填写发件服务器密码"
            clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="发件服务器端口" prop="port">
        <el-input
            v-model.number="form.port"
            placeholder="请填写数字"
            oninput="value=value.replace(/^\.+|[^\d.]/g,'')"
            clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="是否启用ssl" prop="sslEnable">
        <el-select
            v-model="form.sslEnable"
            placeholder="请填写是否启用ssl"
            class="handle-select mr10"
            clearable
        >
          <el-option
              v-for="item in $enum.getMapItems('yesNo')"
              :key="item.value"
              :label="item.name"
              :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
            v-model="form.status"
            placeholder="请填写状态"
            class="handle-select mr10"
            clearable
        >
          <el-option
              v-for="item in $enum.getMapItems('status')"
              :key="item.value"
              :label="item.name"
              :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
            size="small"
            v-model="form.remark"
            placeholder="请填写备注"
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4 }"
            show-word-limit
            maxlength="99"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" size="small" @click="saveEdit"
        >保存
        </el-button
        >
        <el-button size="small" @click="goBack">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import {
  mailConfigDetailApi,
  mailConfigEditApi,
  mailConfigSaveApi
} from "../../api/setup/mailConfig";
import {createObjectBase, deepCopy} from "@/utils";

const obj = {
  id: "",
  name: "",
  nickName: "",
  host: "",
  username: "",
  password: "",
  port: null,
  status: "ENABLE",
  sslEnable: true,
  remark: ""
};

export default {
  data() {
    return {
      form: deepCopy(obj),
      rules: {
        name: [
          {required: true, message: "请填写名称", trigger: "change"},
          {max: 20, message: "长度不能超过20个字符", trigger: "change"},
        ],
        nickName: [
          {required: true, message: "请填写发件人昵称", trigger: "change"},
          {max: 20, message: "长度不能超过20个字符", trigger: "change"},
        ],
        host: [
          {required: true, message: "请填写发件服务器主机", trigger: "change"},
          {max: 50, message: "长度不能超过50个字符", trigger: "change"},
        ],
        username: [
          {required: true, message: "请填写发件服务器账户", trigger: "change"},
          {max: 50, message: "长度不能超过50个字符", trigger: "change"},
        ],
        password: [
          {required: true, message: "请填写发件服务器密码", trigger: "change"},
          {max: 99, message: "长度不能超过99个字符", trigger: "change"},
        ],
        port: [
          {required: true, message: "请填写发件服务器端口", trigger: "change"},
        ],
        remark: [
          {max: 99, message: "长度不能超过99个字符", trigger: "change"},
        ],
      },
    };
  },
  methods: {
    // 编辑操作
    getDetail(id) {
      if (!id) {
        if (this.$refs.form != undefined) {
          this.$refs['form'].resetFields();
        }
      } else {
        mailConfigDetailApi({id}).then((res) => {
          const data = deepCopy(res?.bizData);
          this.form = data;
        });
      }
    },
    saveEdit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          const data = createObjectBase(this.form, Object.keys(obj));
          const {id} = data;
          const fn = id ? mailConfigEditApi : mailConfigSaveApi;
          fn(data).then((res) => {
            this.goBack();
          });
        } else {
          return false;
        }
      });
    },
    goBack() {
      this.$router.goPage({
        path: "/mail/config/page",
      });
    },
  },
  created() {
    const {id} = this.$route.query;
    this.getDetail(id);
  },
};
</script>
